<?php
/**
 * 活动小程序相关服务类
 * Actutils.php BY My
 * Created by sen at 2021/3/23
 * AgeClub.net @ 2021
 */

namespace app\api\controller\bzh\supplier;


use app\admin\model\moyi\log\Like;
use app\api\library\Utils;
use app\common\controller\Api;
use think\Db;
use think\db\Query;
use think\Log;
use think\Validate;
use think\Cache;

class Supplier extends Api
{
    protected $noNeedLogin = ['supplierList','supplierHot','supplierDetail','searchDetail','flow'];
    protected $noNeedRight = '*';



//    产品列表页面
//$page 翻页 $type 分类
    public function supplierList()
    {
        if ($this->request->isGet()) {
            $page = $this->request->request('page', 1);
            $total = Db::name('bzh_supplier')
                ->where('status', 'normal')
                ->count();

            $limit = ($page-1) * 5;
            $list = Db::name('bzh_supplier')
                ->where('status', 'normal')
                ->limit($limit, 5)
                ->select();

            if(empty($list)){
                $this->error('暂无供需',406);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);
            $this->success('获取信息成功', $result, 200);
        }else{
            $this->error('请求方式有误',305);
        }

    }
    /**
     * 获取供需留言
     */
    public function supplierMessage()
    {
        if ($this->request->isPost()) {

//            接收参数供需id
//            留言三个参数
//            插入进去
            $gongxuid=$this->request->post('gongxuaid');
            $message=$this->request->post('message');
        }

    }


    public function supplierdetail()
    {
        if ($this->request->isGet()) {
            $id = $this->request->request('id', '');
            //请求点击量加一
            Db::name('bzh_supplier')->where('id', $id)->setInc('hit');
            $total = Db::name('bzh_supplier')
                ->where('id', $id)
                ->count();

            if ($total == '1') {
                $list = Db::name('bzh_supplier')
                    ->where('id', $id)
                    ->find();
            } else {
                $this->error('暂无该产品', '', 3010);
            }

            $list = str_replace("img",'img width="100%"',$list);
            $list = collection($list)->toArray();
            $coment =[];
            if(!empty($list['message'])){   //分割字符串为数组
                $message = explode('^',$list['message']);

                foreach ($message as $key => $value) {
                    if(!empty($value)){
                        $coment[] = explode('|',$value);
                    }


                }

            }

            $result = array("rows" => $list,"message"=>$coment);
            $this->success('获取产品信息成功', $result, 200);
        }
    }

    /**
     * 获取展会搜索结果
     */
//    public function searchDetail()
//    {
//
//        $name=$this->request->request('name','');
//
//        $total = Db::name('bzh_product')
//            ->where('name','like',$name)
//            ->count();
//
//        if($total =='0'){
//            $this->error('暂无搜索结果','',3010);
//        }else{
//            $list = Db::name('bzh_product')
//                ->where('name','like',$name)
//                ->field('id,name,proimage,introduce')
//                ->select();
//        }
//
//
//        $list = collection($list)->toArray();
//        $result = array( "rows" => $list);
//        $result= json_encode($result,true);
//        return $result;
//
//    }

    /**
     * 获取展会搜索结果
     *
     */
    public function taxonomy()
    {
        if ($this->request->isGet()) {
            $list = Db::name('bzh_taxonomy')
                ->field('id,taxonomy')
                ->select();
            if(empty($list)){
                $this->error('获取分类失败', '', 3010);
            }
            $list = collection($list)->toArray();
            $result = array("rows" => $list);
            $this->success('获取分类信息成功', $result, 200);
        }
    }


//    public function flow()
//    {
//        if ($this->request->isPost()) {
//            $type=$this->request->post('type');//这个选择表名
//            $user_id=$this->request->post('user_id');
//            $id=$this->request->post('id');
//            $flow=$this->request->post('flow');
//            if (!empty($flow)){//传过来的参数就是取消关注
//                $status = 'hidden';
//                $data = [
//                    'user_id' => $user_id,
//                    'gx_id' => $id,
//                    'lei' => $type,
//                    'status'=> $status,
//                ];
//
//                $res = Db::name('bzh_attention')
//                    ->where('user_id',$user_id)
//                    ->where('lei',$type)
//                    ->where('gx_id',$id)
//                    ->update(['status' => "hidden"]);
//
//                $this->success('取消关注成功');
//            }
//
//            if(!empty($id) and !empty($type) and !empty($user_id) ){
//
//                $list = Db::name('bzh_attention')
//                    ->where('user_id',$user_id)
//                    ->where('lei',$type)
//                    ->where('gx_id',$id)
//                    ->count();
//
//
//                if(  $list =='0'){
//                    $status = 'normal';
//                    $data = [
//                        'user_id' => 1,
//                          'gx_id' => 1,
//                           'lei' => 1,
//                    ];
//                   $sql=  Db::name('bzh_attention')->insert($data);
//                   if($sql) {
//                       $this->success('关注成功');
//                   }
//
//                }
//            }else{
//                $this->error('参数错误');
//            }
//
//
//        }
//
//    }

}
